{% extends "base.html" %}

{% block title %}课程管理{% endblock %}

{% block content %}
<div class="container">
    <div class="row mb-4">
        <div class="col-md-12">
            <h1 class="display-5">课程信息管理</h1>
            <a href="{{ url_for('add_course') }}" class="btn btn-primary btn-sm float-end">
                <i class="fas fa-plus me-2"></i>添加课程
            </a>
        </div>
    </div>
    
    <!-- 搜索栏 -->
    <div class="row mb-4">
        <div class="col-md-6 offset-md-3">
            <form method="get" action="{{ url_for('courses') }}" class="d-flex">
                <input type="text" name="query" class="form-control me-2" 
                       placeholder="搜索课程编号/名称" value="{{ request.args.get('query', '') }}">
                <button type="submit" class="btn btn-outline-success">
                    <i class="fas fa-search me-2"></i>搜索
                </button>
            </form>
        </div>
    </div>
    
    <!-- 课程表格 -->
    <div class="row">
        <div class="col-md-12">
            <div class="card">
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-striped table-hover">
                            <thead>
                                <tr>
                                    <th>课程编号</th>
                                    <th>课程名称</th>
                                    <th>学分</th>
                                    <th>授课教师</th>
                                    <th>选课人数</th>
                                    <th>操作</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for course in courses.items %}
                                <tr>
                                    <td>{{ course.course_id }}</td>
                                    <td>{{ course.name }}</td>
                                    <td>{{ course.credit }}</td>
                                    <td>{{ course.teacher }}</td>
                                    <td>{{ course.scores|length }}</td>
                                    <td>
                                        <div class="btn-group" role="group">
                                            <a href="{{ url_for('edit_course', id=course.id) }}" class="btn btn-sm btn-info">
                                                <i class="fas fa-edit me-1"></i>编辑
                                            </a>
                                            <a href="{{ url_for('course_scores', id=course.id) }}" class="btn btn-sm btn-secondary">
                                                <i class="fas fa-list-alt me-1"></i>成绩
                                            </a>
                                            <form action="{{ url_for('delete_course', id=course.id) }}" method="post" 
                                                  onsubmit="return confirm('确定要删除该课程吗？所有相关成绩记录也将被删除！')">
                                                <button type="submit" class="btn btn-sm btn-danger">
                                                    <i class="fas fa-trash me-1"></i>删除
                                                </button>
                                            </form>
                                        </div>
                                    </td>
                                </tr>
                                {% else %}
                                <tr>
                                    <td colspan="6" class="text-center">暂无课程信息</td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                    
                    <!-- 分页导航 -->
                    <div class="mt-4 d-flex justify-content-between align-items-center">
                        <div class="text-muted">
                            显示第 {{ courses.page }} 页，共 {{ courses.pages }} 页，
                            总计 {{ courses.total }} 条记录
                        </div>
                        <nav aria-label="Page navigation">
                            <ul class="pagination justify-content-end">
                                <li class="page-item {% if not courses.has_prev %}disabled{% endif %}">
                                    <a class="page-link" href="{{ url_for('courses', page=courses.page-1, **request.args) }}" 
                                       tabindex="-1">上一页</a>
                                </li>
                                {% for page_num in courses.iter_pages(left_edge=1, left_current=1, right_current=2, right_edge=1) %}
                                {% if page_num %}
                                <li class="page-item {% if page_num == courses.page %}active{% endif %}">
                                    <a class="page-link" href="{{ url_for('courses', page=page_num, **request.args) }}">{{ page_num }}</a>
                                </li>
                                {% else %}
                                <li class="page-item disabled"><a class="page-link">...</a></li>
                                {% endif %}
                                {% endfor %}
                                <li class="page-item {% if not courses.has_next %}disabled{% endif %}">
                                    <a class="page-link" href="{{ url_for('courses', page=courses.page+1, **request.args) }}">下一页</a>
                                </li>
                            </ul>
                        </nav>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}